/**
 * 列表
 */
organ.organGrid = organ.organGrid || Ext.extend(Ext.grid.GridPanel,{
	 initComponent:function(){
	 	var sm = new Ext.grid.CheckboxSelectionModel({singleSelect:true});
	 	var cm = new Ext.grid.ColumnModel([
        		 sm,new Ext.grid.RowNumberer(),
		         {header:'机构ID',dataIndex:"organId", hidden:true},
		         {header:'部门名称',dataIndex:"organName"},
		         {header:'上级部门',dataIndex:"parentName"}
			         
	    ]);
	    cm.defaultSortable = true;

	    var fields = [
				 {name:"organId"},
				 {name:"organName"},
				 {name:"parentName"},
				 {name:"parentId"}
		];
		var ds = new Ext.data.Store({
				proxy:new Ext.data.HttpProxy({
		        	method: 'POST',
		        	url:'organ/getDataList.jhtml'
		        }),
		        reader:new Ext.data.JsonReader({
		            root:'rows',
		            totalProperty:'results',
		            fields: fields
		        }),
		        baseParams:{
		    	    start:0,
		            limit: 20
		        },
		        autoLoad:true
		});
	    var tbar = [{
	    	text:'添加',tooltip: '添加',iconCls:'add',handler:this.addFn
	    	},'-',{
	    	text:'修改',tooltip: '修改',iconCls:'edit',handler:this.editFn
	    	},'-',{
	    	text:'删除',tooltip: '删除',iconCls:'delete',handler:this.delFn
	    	},'-',{
	    	text:'查询',tooltip: '查询',iconCls:'search',handler:this.searchFn
	    }];
	   Ext.apply(this, {
		 	store:ds,
            sm:sm,
            cm:cm,
            title:'部门机构列表',
            border:false,
            region : 'center',
    		stripeRows:true,
            loadMask: {msg:'正在加载数据，请稍候....'},
			layout:'fit',
            bbar:new Ext.PagingToolbar({
                pageSize: 20,
                store:ds,
                displayInfo:true
            }),
            tbar:tbar,
            formWindow: new organ.win()
		 });
	 organ.organGrid.superclass.initComponent.call(this);
	},
	//add
	addFn:function(){
		var addWin = this.ownerCt.ownerCt.formWindow;
		if(!addWin){
			addWin = new organ.win();
		}
		addWin.setIconClass('add');															
		addWin.setTitle('添加部门机构');																	
		addWin.show();	
		addWin.findByType('form')[0].form.reset();
	},
	//edit
	editFn:function(){
		var grid = this.ownerCt.ownerCt;
		var record = grid.getSelectionModel().getSelected();			
		if (record == null) {Ext.Msg.alert('提示','请选择一条记录！');return;}	
		var editWin = grid.formWindow;
		if(!editWin){
			editWin = new organ.win();
		}
	  	editWin.setIconClass('edit');														
	  	editWin.setTitle('编辑部门机构');																
	  	editWin.show();
	  	var win_form = editWin.findByType('form')[0];
	  	win_form.form.reset();												
	  	win_form.form.loadRecord(record);
	},
	//del
	delFn:function(button,v){
		var grid = this.ownerCt.ownerCt;
		var record = grid.getSelectionModel().getSelected();			
		if (record == null) {Ext.Msg.alert('提示','请选择一条记录！');return;}											
		Ext.Msg.confirm('提示', '确定要删除这条记录吗?', function (btn, text) {											
			if (btn == 'yes') {																					
			 var waiting = Ext.Msg.wait('正在处理，请稍等...', '', '');												
				Ext.Ajax.request({																				
					url:'organ/delOrganByOrganId.jhtml',																		
					params:{id:record.data.id},																	
					method:'POST',																				
					success:function(response, options){														
						if(waiting!=null){waiting.hide();}														
						var json = Ext.decode(response.responseText);											
						if(json.success==true){																	
							Ext.Msg.alert('提示',json.msg,function(){												
								grid.getStore().reload();											
							});																					
						}else{																					
							Ext.Msg.alert('提示','发生异常！');														
						}																						
					},																							
					failure:function(response, options){														
						if(waiting!=null){waiting.hide();}														
						var json = Ext.decode(response.responseText);											
						Ext.Msg.alert('提示',json.msg==''?'发生异常！':json.msg);															
					}																							
				});																								
			}																									
		});		
	},
	searchFn:function(){
		
	}
});